

#import "LLChangeUsernameViewController.h"
#import "LLUserInfo.h"
@interface LLChangeUsernameViewController ()<UITextFieldDelegate,UINavigationControllerDelegate>
@property (copy, nonatomic) NSArray *specialStringArray;//特殊字符数组
@property (strong, nonatomic) UITextField *nameTxt;//姓名文本
@property (strong, nonatomic) UIButton *saveBtn;//保存按钮

@end

@implementation LLChangeUsernameViewController

- (void)viewWillAppear:(BOOL)animated {
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [super viewWillAppear:animated];
    [SVProgressHUD dismiss];
    self.navigationController.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //TODO: 页面Disappear 启用
    [[IQKeyboardManager sharedManager] setEnable:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
    [self icVoidNavigationAndTabBarMethod];
    [self LLVoidShowBillTabelViewMethod];
}

#pragma mark - 显示导航栏和标签栏方法
- (void)icVoidNavigationAndTabBarMethod{
    [self titleView];
    if (self.type == 1) {
        self.titleLabel.text = @"修改昵称";
    } else if (self.type == 2) {
        self.titleLabel.text = @"修改内容";
    } else if (self.type == 3) {
        self.titleLabel.text = @"修改签名";
    }
    
    self.rightButton.hidden = YES;
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.saveBtn.frame = CGRectMake(LLScreenWidth-65, self.navH-30, 50, 18);
    [self.saveBtn setTitle:@"完成" forState:0];
    self.saveBtn.titleLabel.font = LLRegularFont(15);
//    self.saveBtn.userInteractionEnabled = NO;
    [self.saveBtn setTitleColor:LLColorRGBAlpha(255, 1, 1, 0.5) forState:0];
    [self.saveBtn addTarget:self action:@selector(didWithbaocun) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:self.saveBtn];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.saveBtn];
}

#pragma mark - 显示视图操作方法
- (void)LLVoidShowBillTabelViewMethod{
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 10+self.navH, self.view.frame.size.width, 50)];
    aview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:aview];
    
    /** 分割线 */
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, 0.5)];
    lineView.backgroundColor = LLColor(201, 201, 201);
    [aview addSubview:lineView];
    UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 49.5, LLScreenWidth, 0.5)];
    lineView1.backgroundColor = LLColor(201, 201, 201);
    [aview addSubview:lineView1];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 60, 44)];
    titleLabel.textColor = LLColor(51, 51, 51);
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = LLRegularFont(15);
    titleLabel.text = @"昵称";
    [aview addSubview:titleLabel];
    
    self.nameTxt = [[UITextField alloc] initWithFrame:CGRectMake(70, 0, aview.frame.size.width-80, 44)];
    self.nameTxt.textColor = LLColor(51, 51, 51);
    self.nameTxt.textAlignment = NSTextAlignmentLeft;
    self.nameTxt.font = LLRegularFont(15);
    self.nameTxt.placeholder = @"请修改您的昵称";
    self.nameTxt.tag = 0;
    self.nameTxt.tintColor = LLMainColor;
    self.nameTxt.delegate = self;
    [self.nameTxt setValue:LLColorRGB153 forKeyPath:@"_placeholderLabel.textColor"];
    self.nameTxt.returnKeyType = UIReturnKeyDone;
    self.nameTxt.clearButtonMode=UITextFieldViewModeWhileEditing;
    [aview addSubview:self.nameTxt];
    [self.nameTxt addTarget:self action:@selector(textField1TextChange:) forControlEvents:UIControlEventEditingChanged];
    
    if (self.type == 1) {
        titleLabel.text = @"昵称";
        self.nameTxt.placeholder = @"请修改您的昵称";
    } else if (self.type == 2) {
        titleLabel.text = @"内容";
        self.nameTxt.placeholder = @"请修改您的内容";
    } else if (self.type == 3) {
        titleLabel.text = @"签名";
        self.nameTxt.placeholder = @"请修改您的签名";
    }
}

#pragma mark - 键盘隐藏方法
-(void)hideKeyboard{
    if ([self.nameTxt isFirstResponder]) {
        [self.nameTxt resignFirstResponder];
    }
}

#pragma mark -UITextFieldDelete
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField isFirstResponder]) {
        return [textField resignFirstResponder];
    }
    return YES;
}

-(void)textField1TextChange:(UITextField *)textField{
    if (textField.markedTextRange == nil) {
        if (textField.tag == 0) {
            if (textField.text.length == 0 ) {
                self.saveBtn.userInteractionEnabled = NO;
                [self.saveBtn setTitleColor:LLColorRGBAlpha(255, 1, 1, 0.5) forState:0];
            } else {
                self.saveBtn.userInteractionEnabled = YES;
                [self.saveBtn setTitleColor:LLColorRGBAlpha(255, 1, 1, 1) forState:0];
            }
        }
    }
}

#pragma mark -保存
- (void)didWithbaocun{
     [self.nameTxt resignFirstResponder];
    if (self.nameTxt.text.length == 0) {
        [self showSVProgressHUDInfoWithsStatus:@"请输入要修改的内容~"];
        [SVProgressHUD dismissWithDelay:1];
        return;
    }
    if (self.changeBlock) {
        self.changeBlock(self.type, self.nameTxt.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
//    [self sendModityRequst:muDic.copy];
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)dealloc {
    self.navigationController.delegate = nil;
    [LLNotificationCenter removeObserver:self];
}
@end
